package com.hospital.register.core.controller.admin;

import com.hospital.register.common.result.Result;
import com.hospital.register.core.service.HospitalService;
import com.hospital.register.dto.core.common.HospitalPageDto;
import com.hospital.register.vo.core.admin.HospitalAndRule;
import com.hospital.register.vo.core.admin.HospitalPageVo;
import com.hospital.register.vo.core.common.HospitalQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Api(tags = "医院管理", description = "面向管理员")
@RestController
@RequestMapping("/admin/hosp/hospital")
@Validated
//@CrossOrigin
public class HospitalAdminController {

    /**
     * 处理医院相关的业务
     */
    @Autowired
    private HospitalService hospitalService;

    @ApiOperation(value = "分页查询医院信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "path", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "limit", value = "当前页", paramType = "path", dataTypeClass = Integer.class, required = true),
    })
    @GetMapping("/list/{page}/{limit}")
    public Result<Page<HospitalPageVo>> list(
            @PathVariable @Min(value = 1, message = "页码不可小于 1") Integer page,
            @PathVariable @Min(value = 1, message = "每页长度不可小于 1") @Max(value = 50, message = "每页条数不可超过 50") Integer limit,
            HospitalPageDto dto) {
        dto.setPage(page);
        dto.setLimit(limit);
        Page<HospitalPageVo> resultPage = this.hospitalService.findPage(dto);
        return Result.ok(resultPage);
    }

    @ApiOperation(value = "更新医院上线状态")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "id", dataTypeClass = String.class, paramType = "path", required = true),
            @ApiImplicitParam(name = "status", value = "状态（必须是0或1）", dataTypeClass = Byte.class, paramType = "path", required = true)
    })
    @GetMapping("/updateHospStatus/{id}/{status}")
    public Result updateHospStatus(
            @PathVariable @NotBlank(message = "id 不可为空") String id,
            @PathVariable @Range(min = 0L, max = 1L, message = "状态非法") @NotNull(message = "状态不可为空") Byte status) {
        this.hospitalService.updateStatusById(id, status);
        return Result.ok();
    }

    @ApiOperation(value = "查询医院详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "id", dataTypeClass = String.class, paramType = "path", required = true),
    })
    @GetMapping("/showHospDetail/{id}")
    public Result<HospitalAndRule> showHospDetail(
            @PathVariable @NotBlank(message = "id 不可为空") String id) {

        HospitalQueryVo hospital = this.hospitalService.findById(id);
        //外面重新包一层
        HospitalAndRule hospitalAndRule = new HospitalAndRule();

        if (hospital != null) {
            hospitalAndRule.setHospital(hospital);
            hospitalAndRule.setBookingRule(hospital.getBookingRule());
            hospital.setBookingRule(null);
        }

        return Result.ok(hospitalAndRule);
    }

}
